export async function request<T>(options: WechatMiniprogram.RequestOption<string | Record<string, any> | ArrayBuffer>, extend?: {
  successMsg?: string
}) {
  return new Promise<T>((resolve, reject) => {

    // 请求头
    let token = wx.getStorageSync('SYS_TOKEN');

    // 填充 TOKEN
    // if (!token) {
    //   wx.showToast({ title: '当前用户尚未登录', icon: 'none' });
    //   reject();
    // }

    options.header = {
      ...options.header,
      Authorization: `Bearer ${token}`,
    }

    wx.request({
      ...options,
      // 404 啥的也走这方法 真是奇了怪了
      success: (res) => {
        wx.hideLoading()
        if (res.statusCode >= 400) {
          wx.showToast({ title: 'error', icon: "error" })
          reject(res)
          return;
        }
        wx.showToast({ title: extend?.successMsg || 'ok', icon: "success" })
        resolve(res.data as unknown as T)
      },
      fail: (reason) => {
        reject(reason)
      }
    })
  })
}